package com.da.lon.wang.xlg.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.adapter.ClassyBrandAdapter;
import com.da.lon.wang.xlg.bean.BrandResultBean;
import com.da.lon.wang.xlg.bean.BrandResultBean.BrandBean;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListener;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshListView;
import com.da.lon.wang.xlg.util.XLGDataUtils;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.http.AjaxParams;

import java.util.ArrayList;

public class ClassyBrandFragment extends BaseFragment {
    PullToRefreshListView listView;
    ClassyBrandAdapter adapter;
    ArrayList<BrandBean> datas;
    ImageView brand_frush_img;
    LinearLayout brand_content;
    View view;

    @Override
    protected View inflateView(LayoutInflater inflater, ViewGroup var2) {
        view = inflater.inflate(R.layout.classy_type_grid, null);
        return view;
    }

    @Override
    protected void initView() {
        registerBaseReceiver();
        listView = (PullToRefreshListView) view
                .findViewById(R.id.classy_type_grid_list);

        brand_content = (LinearLayout) view.findViewById(R.id.brand_content);
        brand_frush_img = (ImageView) view.findViewById(R.id.frush_img);
        brand_frush_img.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                getData();
            }
        });
        getData();
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("ClassyBrandFragment");
        MobclickAgent.onResume(getActivity());
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("ClassyBrandFragment");
        MobclickAgent.onPause(getActivity());
    }

    /**
     * 获取数据
     */
    private void getData() {
        try {
            if (!MyApplication.isNetworkAvailable(getActivity())) {
                showDialog();
                brand_content.setVisibility(View.GONE);
                brand_frush_img.setVisibility(View.VISIBLE);
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                            dismissDialog();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            } else {
                HttpLoader<BrandResultBean> loader = new HttpLoader<BrandResultBean>(
                        getActivity(), XLGConstants.URL + "Product-brand",
                        BrandResultBean.class);
                AjaxParams params = new AjaxParams();
                params.put("token", XLGDataUtils.encrypt2MD5("Product"
                        + "2015#$%" + "brand"));
                loader.setManagerListener(new OnWebLoadListener<BrandResultBean>() {

                    @Override
                    public void OnStart() {
                        showDialog();
                    }

                    @Override
                    public void OnError(String error) {
                        dismissDialog();
                    }

                    @Override
                    public void onSuccess(BrandResultBean t) {
                        dismissDialog();
                        if (t != null && "000000".equals(t.getCode())) {
                            brand_content.setVisibility(View.VISIBLE);
                            brand_frush_img.setVisibility(View.GONE);
                            datas = t.getResult();
                            adapter = new ClassyBrandAdapter(datas,
                                    getActivity());
                            listView.setAdapter(adapter);
                        } else {
                            brand_content.setVisibility(View.GONE);
                            brand_frush_img.setVisibility(View.VISIBLE);
                        }
                    }
                });
                loader.loadData(params);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void RefreshData() {
        // TODO Auto-generated method stub
        getData();
    }


}
